package com.hoshiibuy.purchase.service.impl;

import com.hoshiibuy.purchase.entity.Po;
import com.hoshiibuy.purchase.entity.Podetail;
import com.hoshiibuy.purchase.mapper.PodetailMapper;
import com.hoshiibuy.purchase.service.PodetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.purchase.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Service
public class PodetailServiceImpl extends ServiceImpl<PodetailMapper, Podetail> implements PodetailService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return com.hoshiibuy.purchase.entity.Podetail
    * @description: 根据id查询
    */
    @Cacheable(cacheNames = "Podetail", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "采购- -查询 ")
    public Podetail getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Podetail", allEntries = true)
    @Logable(authName = "采购--新增")
    public boolean save(Podetail entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Podetail", allEntries = true)
    @Logable(authName = "采购-管理-修改")
    public boolean updateById(Podetail entity) {
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询
    */
    @Logable(authName = "采购-管理-分页查询")
    @Cacheable(cacheNames = "Podetail", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<Podetail> page(IPage<Podetail> page, Wrapper<Podetail> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Podetail", allEntries = true)
    @Logable(authName = "采购-管理-删除")
    public boolean deleteById(Podetail entity) {
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }
}
